#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "inc/tm4c123gh6pm.h"



	uint32_t ui8PinData;
	uint32_t ui32ADC0Value[8];

	volatile uint32_t ui32TempAvg;
	volatile uint32_t ui32TempValueC;
	volatile uint32_t ui32TempValueF;



/////// Occurs on the timer overflow
void Timer0IntHandler(void)
{
	ui8PinData = 6;

	uint32_t ui32Period = (SysCtlClockGet() / 10) / 2; //// Set the new timer
	TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1);


	TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); /// Clear the interrupt for timer


	   ADCIntClear(ADC0_BASE, 0); //// Clear interrupt for adc
	   ADCProcessorTrigger(ADC0_BASE, 0); ////// ////// Starts the sample sequencer

	   while(!ADCIntStatus(ADC0_BASE, 0, false)) ////// Stalls until it gets its ADC sample back
	   {
	   }

	   ADCSequenceDataGet(ADC0_BASE, 0, ui32ADC0Value);/// Gets the data acquired
	   ui32TempAvg = (ui32ADC0Value[0]+ui32ADC0Value[1]+ui32ADC0Value[2]+ui32ADC0Value[3]+ui32ADC0Value[4]+ui32ADC0Value[5]+ui32ADC0Value[6]+ui32ADC0Value[7]+ 2) / 8; //// Average 8
	   ui32TempValueC = (1475 - ((2475 * ui32TempAvg)) / 4096)/10;
	   ui32TempValueF = ((ui32TempValueC * 9) + 160) / 5;

	   if(ui32TempValueF>70) //// If hot enough, light Pin
	   GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, ui8PinData);
	   else  GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);

}





int main(void)
{



	SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); // System clock of 400MHz/5 = 80MHz

	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); ////// Enables the ADC0

	ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0); /////// Configures the triger source. Set for processor trigger.

	//// Configures it for 8 samples, and read from temperature
	ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_TS);
	ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_TS);
	ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_TS);
	ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_TS);
	ADCSequenceStepConfigure(ADC0_BASE, 0, 4, ADC_CTL_TS);
	ADCSequenceStepConfigure(ADC0_BASE, 0, 5, ADC_CTL_TS);
	ADCSequenceStepConfigure(ADC0_BASE, 0, 6, ADC_CTL_TS);
	ADCSequenceStepConfigure(ADC0_BASE,0,7,ADC_CTL_TS|ADC_CTL_IE|ADC_CTL_END);


	ADCSequenceEnable(ADC0_BASE, 0);


	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); ///// Enable PortF
	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); ////// Set pin1 (red LED) as out

	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); ///// Enable the timer
	TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); ////// Set the timer method

	uint32_t ui32Period;
	ui32Period = (SysCtlClockGet() / 10) / 2; // This is the frequency. /10 /2, means 20 interrupts, but a frequency cycle is high to low, then low to high, which requires 2 interrupts, which is 10 HZ.
	ui32Period = (SysCtlClockGet() / 30) / 2; // Setting it to /30 / 2, gives 30HZ with 50% duty (equal).
	TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1);

	IntEnable(INT_TIMER0A); /// Enable interrupt for timer0A
	TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); /// Set the interrupt feature for Timer 0a
	IntMasterEnable(); ///// Enable the master interrupt

	TimerEnable(TIMER0_BASE, TIMER_A); /////// Enable the timer





	while(1)
	{

	}
}
